﻿Imports System.Data.SqlClient
Imports System.Data.Odbc

Public Class frmCatalogoAlmacenes
    Dim Bitacora As New clsActividad
    Dim Usuario As New clsUsuarioPrincipal
    Private mstrCondicion As String
    Public WriteOnly Property Condicion() As String
        Set(ByVal value As String)
            mstrCondicion = value
        End Set
    End Property

    Private Sub frmCatalogoAlmacenes_FormClosed(sender As Object, e As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed
        Bitacora.RegistraActividad("Cerró al catálogo de Almacenes")
        Usuario.Ubicacion("Ventana Principal")
    End Sub
    Private Sub frmCatalogoAlmacenes_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        ToolStrip1.Cursor = Cursors.Hand
        ToolStrip1.RenderMode = System.Windows.Forms.ToolStripRenderMode.System

        dgAlmacenes.DefaultCellStyle.Font = New Font("Segoe UI", 10, FontStyle.Regular)

        Bitacora.RegistraActividad("Ingresó al catálogo de Almacenes")
        Usuario.Ubicacion("Catálogo de Almacenes")

        txtBuscar.Clear()
        CargaAlmacenes()
        txtBuscar.Focus()
        dgAlmacenes.Columns(1).ReadOnly = False
        dgAlmacenes.Columns(4).DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
    End Sub
    Sub CargaAlmacenes()
        Windows.Forms.Cursor.Current = Cursors.WaitCursor
        dgAlmacenes.Rows.Clear()

        Dim Filtro As String
        Filtro = UCase(txtBuscar.Text)
        Filtro = "%" & Filtro.Replace(" ", "%") & "%"

        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader

        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select * from Almacenes WHERE UPPER(cCodigoA01) LIKE '" & Filtro & "' " & mstrCondicion & " OR UPPER(cNombreA01) LIKE '" & Filtro & "' " & mstrCondicion

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader


            Do While rdBuscar.Read()
               
                dgAlmacenes.Rows.Add(rdBuscar("IdAlmacen"), False, rdBuscar("cCodigoA01").ToString.Trim, rdBuscar("cNombreA01").ToString.Trim, "Si")
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

    End Sub

    Private Sub ToolStripButton1_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton1.Click
        Close()

    End Sub

    Private Sub RegresarToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles RegresarToolStripMenuItem.Click
        Close()

    End Sub

    Private Sub AdminPAQToolStripMenuItem1_Click(sender As System.Object, e As System.EventArgs) Handles AdminPAQToolStripMenuItem1.Click
        Importar()

    End Sub
    Sub Importar()
        Dim cnConn As New SqlConnection
        Dim strSql As String
        Dim cmdBuscar As New SqlCommand
        Dim rdBuscar As SqlDataReader
        Dim Almacen As New clsCatalogoAlmacenes


        Try
            cnConn.ConnectionString = CitraConnection
            strSql = "Select cIdAlmacen,cCodigoA01,cNombreA01 FROM MGW10003"

            cmdBuscar.Connection = cnConn
            cmdBuscar.CommandText = strSql


            cnConn.Open()
            rdBuscar = cmdBuscar.ExecuteReader


            Do While rdBuscar.Read()
                Almacen.IdAlmacen = rdBuscar("cIdAlmacen")
                Almacen.cCodigoA01 = rdBuscar("cCodigoA01").ToString.Trim
                Almacen.cNombreA01 = rdBuscar("cNombreA01").ToString.Trim
                Almacen.Guardar()
                Bitacora.RegistraActividad("Importó el almacen " & Almacen.cNombreA01)
            Loop
        Catch ex As Exception
            MessageBox.Show(ex.Message, "System Message", MessageBoxButtons.OK, MessageBoxIcon.Error)
        Finally
            If cnConn.State = ConnectionState.Open Then
                cnConn.Close()
            End If
        End Try

        CargaAlmacenes()

    End Sub

    Private Sub DesdeAdminPAQToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles DesdeAdminPAQToolStripMenuItem.Click
        Importar()

    End Sub

    Private Sub txtBuscar_TextChanged(sender As System.Object, e As System.EventArgs) Handles txtBuscar.TextChanged
        CargaAlmacenes()

    End Sub
    Sub ImprimeFiltro()
        Dim xl As Object
        Dim wb As Object
        Dim i As Integer
        xl = CreateObject("Excel.Application")
        xl.Visible = True
        xl.Workbooks.Add()
        wb = xl.activeworkbook


        wb.SHEETS(1).cells(1, 1).value = "Imprimiendo..."
        wb.SHEETS(1).columns(1).columnwidth = 15

        Dim rng As Integer = 5

        For i = 2 To 4
            wb.SHEETS(1).cells(rng, i).FONT.name = "Segoe UI Light"
            wb.SHEETS(1).cells(rng, i).font.bold = True
            wb.SHEETS(1).CELLS(rng, i).HorizontalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).VerticalAlignment = -4108
            wb.SHEETS(1).CELLS(rng, i).WrapText = True
            wb.SHEETS(1).CELLS(rng, i).Orientation = 0
            wb.SHEETS(1).CELLS(rng, i).AddIndent = False
            wb.SHEETS(1).CELLS(rng, i).IndentLevel = 0
            wb.SHEETS(1).CELLS(rng, i).ShrinkToFit = False
            wb.SHEETS(1).CELLS(rng, i).MergeCells = False
            wb.SHEETS(1).CELLS(rng, i).ReadingOrder = -5002
        Next
        wb.SHEETS(1).cells(rng, 2).value = "Código"
        wb.SHEETS(1).cells(rng, 3).value = "Nombre"
        wb.SHEETS(1).cells(rng, 4).value = "Interfaz AdminPAQ"
        wb.SHEETS(1).columns(2).columnwidth = 10
        wb.SHEETS(1).columns(3).columnwidth = 40
        wb.SHEETS(1).columns(4).columnwidth = 12

        rng = rng + 1
        For i = 0 To dgAlmacenes.Rows.Count - 1
            For j = 2 To 4
                wb.SHEETS(1).cells(rng, j).FONT.name = "Segoe UI Light"

            Next
            wb.SHEETS(1).cells(rng, 2).value = dgAlmacenes.Rows(i).Cells(2).Value
            wb.SHEETS(1).cells(rng, 3).value = dgAlmacenes.Rows(i).Cells(3).Value.ToString.Trim
            wb.SHEETS(1).cells(rng, 4).value = dgAlmacenes.Rows(i).Cells(4).Value.ToString.Trim
           
            rng = rng + 1
        Next

        wb.SHEETS(1).cells(1, 2).value = EmpresaActiva
        wb.SHEETS(1).cells(1, 2).FONT.SIZE = 20
        wb.SHEETS(1).cells(1, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(2, 2).value = "Vista previa del Catálogo de Almacenes"
        wb.SHEETS(1).cells(2, 2).FONT.SIZE = 14
        wb.SHEETS(1).cells(2, 2).FONT.name = "Segoe UI Light"
        wb.SHEETS(1).cells(3, 2).value = "Impreso el " & Now().Day & "-" & Now().Month & "-" & Now().Year & " por " & UsuarioActivo
        wb.SHEETS(1).cells(3, 3).FONT.SIZE = 12
        wb.SHEETS(1).cells(3, 3).FONT.name = "Segoe UI Light"

        wb.SHEETS(1).cells(1, 1).value = ""
        wb.SHEETS(1).columns(1).columnwidth = 3
        wb.sheets(1).cells(1, 1).interior.color = 15773696
        wb.sheets(1).cells(2, 1).interior.color = 15773696

    End Sub

    Private Sub ToolStripButton2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripButton2.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de almacenes")
        ImprimeFiltro()
    End Sub

    Private Sub ExportarFiltroAExcelToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ExportarFiltroAExcelToolStripMenuItem.Click
        Bitacora.RegistraActividad("Imprimió el filtro activo del catálogo de almacenes")
        ImprimeFiltro()
    End Sub
End Class